package com.ynet.middleground.mobile.approve.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSSClient;
import com.ynet.file.service.FileService;
import com.ynet.file.starter.FileServiceConfiguration;

/**
 * @author liulx
 * @description 通用工具类
 * @date 2020-04-21 22:41
 */
@Component
public class CommUtils {

    @Autowired
    FileServiceConfiguration fileServiceConfiguration;

    @Autowired
    FileService fileService;

    /**
     * 根据文件短路径获得文件完整地址
     *
     * @param shortOssUrl 文件短路径地址 {@link FileService#uploadFile(String, java.io.InputStream, String, String)} eg:
     *            zhogntai/test.pdf
     * @return oss 完整路径 eg: http://oss-cn-qingdao-qdg-d01-a.cloud.qdport.com/zhongtai/test.pdf
     */
    public String getOssFullUrl(String shortOssUrl) {
        String endpoint = fileServiceConfiguration.getConfigs().get(0).getOssConfig().getEndpoint();
        return endpoint.substring(0, endpoint.lastIndexOf("/")) + "/" + shortOssUrl;
    }

    /**
     * 获取 oss bucketName
     *
     * @return bucketName
     */
    public String getOssBucketName() {
        OSSClient ossClient = (OSSClient)fileService.getOriginClient("oss");
        String path = ossClient.getEndpoint().getPath();
        return path.substring(path.indexOf("/") + 1);
    }

    /**
     * 将毫秒数，转化成（*天*小时*分*秒*毫秒）
     * 
     * @param ms
     * @return
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        if (minute > 0) {
            sb.append(minute + "分");
        }
        if (second > 0) {
            sb.append(second + "秒");
        }
        if (milliSecond > 0) {
            sb.append(milliSecond + "毫秒");
        }
        return sb.toString();
    }

    /**
     * 获取某天的开始时间00时00分00秒
     * 
     * @param time
     * @return
     * @throws ParseException
     */
    public static Date dateBegin(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        time = time + " 00:00:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取某天的开始时间23时59分59秒
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static Date dateEnd(String time) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        time = time + " 23:59:59";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 判断是不是数字
     * 
     * @param str
     * @return
     */
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

}
